package com.swak.frame.job;


import com.swak.frame.job.annotation.EasyJobInfo;
import com.swak.frame.job.mapper.EasyJobMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * The type Job trigger pool handler.
 *
 * @Author: yuanchao.ma
 * @date: 2022 /8/24 15:46
 */
@Slf4j
public class JobTriggerPoolHandler {
    private final EasyJobTrigger easyJobTrigger;
    private ThreadPoolExecutor triggerPool = null;

    private final  JobScheduleHandler jobScheduleHandler;

    private final EasyJobMapper easyJobMapper;

    /**
     * Instantiates a new Job trigger pool handler.
     *
     * @param jobScheduleHandler the job schedule handler
     */
    public JobTriggerPoolHandler(JobScheduleHandler jobScheduleHandler,EasyJobMapper easyJobMapper) {
        this.jobScheduleHandler = jobScheduleHandler;
        this.easyJobMapper = easyJobMapper;
        this.easyJobTrigger = new EasyJobTrigger(jobScheduleHandler);
    }

    /**
     * Start.
     */
    public void start() {
        triggerPool = new ThreadPoolExecutor(
                5,
                15,
                60L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(2000),
                r -> new Thread(r, "easyJob-Pool-" + r.hashCode()));
    }


    /**
     * Stop.
     */
    public void stop() {
        triggerPool.shutdownNow();
        log.debug(">>>>>>>>> easy-job trigger thread pool shutdown success.");
    }

    /**
     * Add trigger.
     *
     * @param jobInfo the job info
     */
    public void addTrigger(EasyJobInfo jobInfo) {
        triggerPool.submit(() -> {
            try {
                // do trigger
                easyJobTrigger.processTrigger(jobInfo);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
    }

    /**
     * Trigger.
     *
     * @param jobName the job name
     */
    public  void trigger(String jobName) {
       EasyJobInfo jobInfo =  easyJobMapper.findByJobName(jobName);
       if(!Objects.isNull(jobInfo)){
           this.addTrigger(jobInfo);
       }else {
           log.warn("easy-job not found,jobName:{}", jobName);
       }
    }
}
